package com.ayu.wxgzh.ayucyy.chain.handler;

import com.ayu.wxgzh.ayucyy.entity.BaseMessage;
import com.ayu.wxgzh.ayucyy.entity.TextMessage;
import com.ayu.wxgzh.ayucyy.service.ChatGPTService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author 徐林玉
 * @Description ChatGPTHandler
 * @Date 2023/2/6 14:46
 * @Version 1.0
 */
@Component
@Slf4j
public class ChatGPTHandler extends AbstractHandler {


    private final ChatGPTService chatGPTService;

    @Autowired
    public ChatGPTHandler(ChatGPTService chatGPTService) {
        this.chatGPTService = chatGPTService;
    }


    @Override
    public BaseMessage handle(Map<String, String> requestMap) {
        //降级：如果配置了降级，则跳过此处理器，执行下一个处理器
        if (super.getConfig().getDown()) {
            log.warn("ChatGPT ChatGPTHandler 已降级，跳过ChatGPT ChatGPTHandler...");
            return super.next(requestMap);
        }

        String content = requestMap.get("Content");
        String fromUser = requestMap.get("FromUserName");
        String reply = chatGPTService.reply(content, fromUser);
        if(StringUtils.isNotEmpty(reply)){

            return new TextMessage(requestMap,reply);
        }

        return super.next(requestMap);

    }
}
